using MeiluluSqlite.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MeiluluSqlite
{
    /// <summary>
    /// 
    /// </summary>
    public class DB : SqliteTable
    {
        public string DB_Path { get; set; }

        public DB()
        {
            DB_Path = System.IO.Directory.GetCurrentDirectory() + "\\db_log";
            DB_SqlLite.CreateDB(DB_Path);
        }
        public DB(string path)
        {
            DB_Path = path;
            DB_SqlLite.CreateDB(DB_Path);
        }
        public T Select<T>(Func<T> fun)
        {
            throw new NotImplementedException();
        }

        public T Select<T>()
        {
            throw new NotImplementedException();
        }

        public bool Updata<T>(T t)
        {
            if (t == null)
                return false;
            Type type = t.GetType();
            string sqltr = "UPDATE " + type.Name + " set ";
            string upValue = "";
            string condition = " where ";
            var list = type.GetProperties();
            for (int i = 0; i < list.Length; i++)
            {
                var attributes = list[i].GetCustomAttributes(typeof(ModeSign), true);
                if (attributes.Length > 0)
                {
                    if (((ModeSign)attributes[0]).PrimaryKey)
                    {
                        condition += list[i].Name + "='" + list[i].GetValue(t, null).ToString() + "'";
                    }
                    else
                    {
                        if (upValue != "")
                        {
                            upValue += ",";
                        }
                        upValue += list[i].Name + "='" + list[i].GetValue(t, null)?.ToString() + "'";
                    }
                }
            }

            return DB_SqlLite.ExecuteNonQuery(DB_Path, sqltr + upValue + condition);
        }

        public bool Insert<T>(T t)
        {
            if (t == null)
                return false;
            Type type = t.GetType();
            string sqltr = "INSERT INTO " + type.Name;
            string attribute = "(";
            string value = "VALUES(";
            var list = type.GetProperties();
            type.GetFields();
            for (int i = 0; i < list.Length; i++)
            {
                if (i > 0)
                {
                    attribute += ",";
                    value += ",";
                }
                attribute += list[i].Name;
                value += "'" + list[i].GetValue(t, null)?.ToString() + "'";
            }
            attribute += ")";
            value += ")";
            return DB_SqlLite.ExecuteNonQuery(DB_Path, sqltr + attribute + value);
        }

        public bool Delate<T>(T t)
        {
            if (t == null)
                return false;
            Type type = t.GetType();
            string sqltr = "DELETE FROM " + type.Name + " where ";
            var list = type.GetProperties();
            for (int i = 0; i < list.Length; i++)
            {
                var attributes = list[i].GetCustomAttributes(typeof(ModeSign), true);
                if (attributes.Length > 0)
                {
                    if (((ModeSign)attributes[0]).PrimaryKey)
                    {
                        sqltr += list[i].Name + "='" + list[i].GetValue(t, null)?.ToString() + "'";
                        return DB_SqlLite.ExecuteNonQuery(DB_Path, sqltr);
                    }
                }
            }
            return false;
        }

        public bool CreatTable(Type type)
        {
            string sqlstr = "CREATE TABLE " + type.Name + "(";
            var list = type.GetProperties();
            for (int i = 0; i < list.Length; i++)
            {
                if (i > 0)
                {
                    sqlstr += ",";
                }
                sqlstr += list[i].Name + " varchar(255)";
            }
            sqlstr += ")";
            return DB_SqlLite.CreateTable(DB_Path, sqlstr, type.Name);
        }

        public List<T> Select<T>(string where)
        {
            var type = typeof(T);
            string sqlstr = "select * from " + type.Name;
            if (where != "")
            {
                sqlstr += " where " + where;
            }
            return DB_SqlLite.SelectBySql<T>(DB_Path, sqlstr);
        }

        public bool Updata<T>(T t, params string[] par)
        {
            if (t == null)
                return false;
            Type type = t.GetType();
            string sqltr = "UPDATE " + type.Name + " set ";
            string upValue = "";
            string condition = " where ";
            var list = type.GetProperties();
            for (int i = 0; i < list.Length; i++)
            {
                var attributes = list[i].GetCustomAttributes(typeof(ModeSign), true);
                if (attributes.Length > 0)
                {
                    if (((ModeSign)attributes[0]).PrimaryKey)
                    {
                        condition += list[i].Name + "='" + list[i].GetValue(t, null).ToString() + "'";
                        break;
                    }
                }
            }
            for (int i = 0; i < par.Length; i++)
            {
                if (upValue != "")
                {
                    upValue += ",";
                }
                var pro = type.GetProperty(par[i]);
                upValue += pro.Name + "='" + pro.GetValue(t) + "'";
            }
            return DB_SqlLite.ExecuteNonQuery(DB_Path, sqltr + upValue + condition);
        }
    }
}
